package com.woc.cloud.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woc.cloud.api.constant.ErrorCode;
import com.woc.cloud.api.entity.Response;
import com.woc.cloud.srv.model.WocSite;
import com.woc.cloud.srv.service.WocSiteService;

/**
 * 站点管理控制器
 * 
 * @author     woc
 * @date       2024-01-26 
 * @version    0.0.1
 */
@RestController
@RequestMapping(value = {"", "/site"})
public class SiteController {

    @Autowired
    private WocSiteService wocSiteService;

    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public Object list(@RequestParam(required = false, defaultValue = "0") Integer offset,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        QueryWrapper<WocSite> wrapper = new QueryWrapper<>();
        Map<String, Object> result = new HashMap<>();
        result.put("total", wocSiteService.count(wrapper));
        wrapper.orderByDesc("sid");
        wrapper.last(" limit " + offset + "," + limit);
        result.put("rows", wocSiteService.list(wrapper));
        return result;
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public Response<Object> info(Integer sid) {
    	Response<Object> resp = new Response<>();
        if (sid == null || sid <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        WocSite site = wocSiteService.getById(sid);
        if (site == null) {
            return resp.withError("站点查询失败");
        }
        return resp.withOk(site);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> save(@Validated @RequestBody WocSite site) {
        Response<Object> resp = new Response<>();
        if (site == null || StringUtils.isBlank(site.getName())) {
            return resp.withError(ErrorCode.ParamsError);
        }
        QueryWrapper<WocSite> wrapper = new QueryWrapper<>();
        wrapper.eq("name", site.getName());
        WocSite exist = wocSiteService.getOne(wrapper);
        if (site.getSid() == null) {
            if (exist != null) {
                return resp.withError("站点不能重复");
            }
            if (!wocSiteService.save(site)) {
                return resp.withError("站点新增失败");
            }
        } else {
            if (exist != null && !exist.getSid().equals(site.getSid())) {
                return resp.withError("站点不能重复");
            }
            if (!wocSiteService.updateById(site)) {
                return resp.withError("站点更新失败");
            }
        }
        return resp.withOk();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> delete(Integer sid) {
        Response<Object> resp = new Response<>();
        if (sid == null || sid <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        if (!wocSiteService.removeById(sid)) {
            return resp.withError("站点删除失败");
        }
        return resp.withOk();
    }
}
